package com.fulihui.information.biz.content.impl.handler;

import com.alibaba.fastjson.JSONObject;
import com.fulihui.information.core.repository.ContentCommentsPraiseRepository;
import com.fulihui.information.core.repository.ContentCommentsReplyRepository;
import com.fulihui.information.core.repository.ContentCommentsRepository;
import com.fulihui.information.dal.dataobj.InfoContentCommentsPraise;
import com.fulihui.information.dal.dataobj.InfoContentCommentsReply;
import com.fulihui.information.dto.InfoContentCommentsDTO;
import com.fulihui.information.dto.InfoContentCommentsReplyDTO;
import com.fulihui.mscore.api.MessageService;
import com.fulihui.mscore.dto.MessageJson;
import com.fulihui.mscore.request.MsgCreateRequest;
import com.google.common.collect.Maps;
import org.apache.commons.codec.binary.Base64;
import org.near.servicesupport.request.RequestBuilder;
import org.near.servicesupport.request.TRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by LM on 2017/4/21.
 */
@Component
public class ContentCommentsHandler {

    private final static Logger LOGGER = LoggerFactory
            .getLogger(ContentCommentsHandler.class);

    // 异步业务处理线程池
    public static ExecutorService           es = Executors.newFixedThreadPool(10);

    @Autowired
    private ContentCommentsRepository       commentsRepository;
    @Autowired
    private ContentCommentsReplyRepository  commentsReplyRepository;
    @Autowired
    private ContentCommentsPraiseRepository commentsPraiseRepository;
    @Autowired
    private MessageService                  messageService;

    /**
     * 评论赞
     * @param commentsId
     * @param userId
     * @param contentId
     */
    public void praiseTotal(String commentsId, String userId, String contentId) {
        es.execute(() -> {
            // 叠加评论点赞数
            commentsRepository.praiseTotalAdd(commentsId);
            InfoContentCommentsPraise commentsPraise = new InfoContentCommentsPraise();
            commentsPraise.setContentId(contentId);
            commentsPraise.setCommentId(commentsId);
            commentsPraise.setUserId(userId);
            // 保存点赞记录
            commentsPraiseRepository.insertSelective(commentsPraise);
            /**
             * 发送消息
             */
            // 查询评论
            InfoContentCommentsDTO commentsDTO = commentsRepository.selectByPrimaryKey(commentsId);
            Map<String, Object> dataMap = Maps.newHashMap();
            dataMap.put("targetComments", commentsDTO.getCommentContent());
            MsgCreateRequest request = new MsgCreateRequest();
            request.setMsgType("MESSAGE_THUMBS_UP");
            request.setDataMap(dataMap);
            request.setTarget(commentsDTO.getUserId());
            MessageJson messageJson = new MessageJson();
            messageJson.setCommentUserId(userId);
            messageJson.setCommentId(commentsId);
            messageJson.setCommentdataId(contentId);
            request.setExtendData(JSONObject.toJSONString(messageJson));
            TRequest<MsgCreateRequest> var1 = RequestBuilder.buildT(request);
            messageService.createMsg(var1);
            LOGGER.info("send praise comments msg,toUser={}",userId);

        });
    }

    /**
     * 叠加回复数
     * @param commentsId
     */
    public void replyTotal(String commentsId) {
        es.execute(() -> {
            commentsRepository.replyTotalAdd(commentsId);
        });
    }

    /**
     * 回复消息发送
     * @param commentsReply
     */
    public void sendCommentsMessage(InfoContentCommentsReply commentsReply) {
        es.execute(() -> {
            int storey = commentsReply.getStorey();
            Map<String, Object> dataMap = Maps.newHashMap();
            switch (storey) {
                case 2: // 评论的回复
                    InfoContentCommentsDTO commentsDTO = commentsRepository
                        .selectByPrimaryKey(commentsReply.getCommentId());
                    dataMap.put("targetComments",
                        Base64.encodeBase64String(commentsDTO.getCommentContent().getBytes()));
                    break;
                case 3: // 回复的回复
                    InfoContentCommentsReplyDTO commentsReplyDTO = commentsReplyRepository
                        .selectByPrimaryKey(commentsReply.getReplyId());
                    dataMap.put("targetComments",
                        Base64.encodeBase64String(commentsReplyDTO.getReplyContent().getBytes()));
                    break;
                default:
                    break;
            }
            MsgCreateRequest request = new MsgCreateRequest();
            request.setMsgType("MESSAGE_COMMENT");
            request.setDataMap(dataMap);
            request.setTarget(commentsReply.getToUserId());
            MessageJson messageJson = new MessageJson();
            messageJson.setComments(commentsReply.getReplyContent());
            messageJson.setCommentUserId(commentsReply.getUserId());
            messageJson.setCommentId(commentsReply.getCommentId());
            messageJson.setCommentdataId(commentsReply.getContentId());
            request.setExtendData(JSONObject.toJSONString(messageJson));
            TRequest<MsgCreateRequest> var1 = RequestBuilder.buildT(request);
            messageService.createMsg(var1);
            LOGGER.info("send reply comments msg,toUser={}",commentsReply.getToUserId());
        });
    }

    @PreDestroy
    private void destroy() {
        if (es != null) {
            es.shutdown();
        }
    }

}
